

#include <iostream>
#include <vector>
using namespace std;

class Solution
{
public:
    int lengthOfLongestSubstring(string s)
    {
        int maxlen = 0;
        int cnt[128] = { 0 };

        int sz = s.size();
        for (int left = 0, right = 0; right < sz; ++right)
        {
            cnt[s[right]]++;

            while (cnt[s[right]] > 1)
            {
                cnt[s[left]]--;
                ++left;
            }
            maxlen = max(maxlen, right - left + 1);

            if (sz == maxlen)
                break;
        }

        return maxlen;
    }

    int minSubArrayLen(int target, vector<int>& nums)
    {
        int sz = nums.size();
        int len = sz + 1;

        int sum = 0;
        int left = 0, right = 0;
        while (right < sz)
        {
            sum += nums[right];

            while (sum >= target)
            {
                len = min(len, right - left + 1);
                if (1 == len)
                    break;

                sum -= nums[left++];
            }

            if (1 == len)
                break;

            ++right;
        }

        if (len == sz + 1)
            len = 0;

        return len;
    }

    // err
    int longestOnes(vector<int>& nums, int k)
    {
        int cnt = 0;
        for (auto i : nums)
        {
            if (0 == i)
                ++cnt;
        }

        int sz = nums.size(), len = 0;

        // if (cnt < k)
        //     return sz;

        for (int left = 0; left < sz; ++left)
        {
            int curcnt = 0;
            for (int right = left; right < sz; ++right)
            {
                if (0 == nums[right] && curcnt < k)
                    ++curcnt;

                if (curcnt == k)
                {
                    while (right + 1 < sz && 1 == nums[right + 1])
                    {
                        ++right;
                    }
                    len = max(len, right - left + 1);
                    if (0 == k && 0 == nums[left])
                        len = len == 0 ? 0 : (len - 1);

                    break;
                }
            }
        }

        return len;
    }
};

void Test1()
{
    vector<int> nums{ 0,0,1,1,1,0,0 };
    int k = 0;

    cout << Solution().longestOnes(nums, k) << endl;
}

int main()
{
    Test1();


	return 0;
}